<template>
  <div class="container news">
    <el-row :gutter="10" class="news-header">
      <el-col :span="17">
        <div class="news-banner">
          <swiper :options="swiperOption">
            <swiper-slide
              v-for="(item, index) in newsBanner"
              :key="index"
              @click.native="path(item)"
            >
              <img :src="item.image_url" />
              <!-- <edu-image fit="cover" :lazy="false" :src="item.image_url"></edu-image> -->
            </swiper-slide>
            <div class="swiper-pagination swiper-pagination-white" slot="pagination"></div>
            <div class="swiper-button-prev swiper-button-white" slot="button-prev"></div>
            <div class="swiper-button-next swiper-button-white" slot="button-next"></div>
          </swiper>
        </div>
      </el-col>
      <el-col :span="7">
        <div
          class="news-recommend"
          v-for="(item, index) in newsSplendid"
          :key="index"
          @click="handlePath(item.id)"
        >
          <edu-image :src="item.cover_url" fit="cover"></edu-image>
          <div class="title">{{ item.title }}</div>
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="14" class="news-wrapper">
      <el-col :span="16">
        <div class="news-all">
          <div class="news-category" v-if="categoryList.length > 0">
            <edu-category @patch="handleCategory" :list="categoryList"></edu-category>
          </div>
          <template v-if="newsList != null">
            <div class="all-list" v-if="newsList.length > 0">
              <el-row type="flex" class="item" v-for="item in newsList" :key="item.id">
                <div class="picture" @click="handlePath(item.id)">
                  <edu-image :src="item.cover_url" fit="cover"></edu-image>
                </div>
                <div class="content">
                  <div class="title" @click="handlePath(item.id)">
                    {{ item.title }}
                  </div>
                  <div class="abstract">
                    {{ item.abstract }}
                  </div>
                  <el-row type="flex" align="middle" class="desc">
                    <span>{{ item.publish_time | formatTime('{y}-{m}-{d}') }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span>{{ item.read_count }}人浏览</span>
                  </el-row>
                </div>
              </el-row>
            </div>
            <edu-no-data v-else></edu-no-data>
            <edu-pagination @handleCurrentChange="handleCurrentChange"></edu-pagination>
          </template>
        </div>
      </el-col>
      <el-col :span="8">
        <div class="news-hot">
          <div class="hot-title">热门资讯</div>
          <div class="hot-list">
            <el-row
              type="flex"
              class="item"
              v-for="(item, index) in newsPopular.slice(0, 5)"
              :key="item.id"
            >
              <div class="index" v-if="index < 5">0{{ index + 1 }}</div>
              <div class="content">
                <div class="title" @click="handlePath(item.id)">
                  {{ item.title }}
                </div>
                <div class="abstract">{{ item.abstract }}</div>
                <el-row type="flex" align="middle" class="desc">
                  <span>{{ item.publish_time | formatTime('{y}-{m}-{d}') }}</span>
                  <el-divider direction="vertical"></el-divider>
                  <span>{{ item.read_count }}人浏览</span>
                </el-row>
              </div>
            </el-row>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import EduPagination from '@/components/EduPagination'
import EduNoData from '@/components/EduNoData'
import EduCategory from '@/components/EduCategory'
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
import { mapState } from 'vuex'
export default {
  components: {
    EduPagination,
    EduNoData,
    EduCategory,
    Swiper,
    SwiperSlide
  },
  data() {
    return {
      newsBanner: [],
      newsSplendid: [],
      newsPopular: [],
      newsList: null,
      categoryList: [],
      swiperOption: {
        loop: true,
        autoplay: {
          delay: 5000,
          disableOnInteraction: false
        },
        pagination: {
          el: '.swiper-pagination',
          clickable: true
        },
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev'
        }
      }
    }
  },
  watch: {
    $route(n, o) {
      if (n != o) {
        this.$store.dispatch('paginate/setPaginate', {
          currentPage: 1
        })
        this.handleNewsList()
        this.handleCategoryList()
      }
    }
  },
  computed: {
    ...mapState({
      getGlobalSchoolId: state => state.projectSetting.e_app_mhm_id
    })
  },
  mounted() {
    this.handleNewsInit()
    this.handleNewsList()
    this.handleCategoryList()
    this.handleTopicBanner()
  },
  methods: {
    // 列表分页
    handleCurrentChange() {
      this.handleNewsList()
    },
    // 资讯基础数据
    handleNewsInit() {
      this.$api.getTopicIndex({}, res => {
        this.newsSplendid = res.data.splendid
        this.newsPopular = res.data.popular
      })
    },
    // 获取资讯banner
    handleTopicBanner() {
      this.$api.getConfigBanner(
        {
          type: 'topic',
          mhm_id: this.getGlobalSchoolId
        },
        res => {
          this.newsBanner = res.data
        }
      )
    },
    // 资讯列表数据
    handleNewsList() {
      this.$api.getTopicList(
        {
          page: this.$store.getters['paginate/page'],
          count: 9,
          category: this.$route.query.category
        },
        res => {
          this.newsList = res.data.data
          this.$store.dispatch('paginate/setPaginate', {
            listRows: 9,
            total: res.data.total
          })
        }
      )
    },
    // 跳转
    handlePath(id) {
      this.$router.push({
        path: '/topic/details',
        query: {
          id: id
        }
      })
    },
    // 分类数据
    handleCategoryList() {
      this.$api.getCategory(
        {
          type: 5
        },
        res => {
          let data = res.data
          this.categoryList = data
        }
      )
    },
    // 分类筛选
    handleCategory(val) {
      this.$router.push({
        query: {
          category: val
        }
      })
    },
    // banner跳转
    path(item) {
      if (item.link_type == 1) {
        this.$router.push({
          path: `/course/${item.link_data_id}`
        })
      } else if (item.link_type == 2) {
        window.open(item.link_href, '_blank')
      }
    }
  },
  destroyed() {
    this.$store.commit('paginate/SET_PAGINATE_DEFAULT')
  }
}
</script>

<style lang="scss" scoped>
.news {
  .news-header {
    margin-top: 10px;
    .news-banner {
      height: 370px;
      cursor: pointer;
      .swiper-slide {
        height: 0;
        padding-top: 370px;
        img {
          position: absolute;
          left: 0;
          top: 0;
          width: 100%;
          height: 100%;
          object-fit: cover;
          vertical-align: top;
        }
      }
      .swiper-pagination {
        .swiper-pagination-bullet {
          opacity: 0.48;
          border-radius: 4px;
          background: #fff;
        }
        .swiper-pagination-bullet-active {
          opacity: 1;
        }
      }
      .swiper-button-prev,
      .swiper-button-next {
        opacity: 0;
        outline: none;
        width: 36px;
        height: 36px;
        margin-top: calc(-1 * 36px / 2);
        border-radius: 50%;
        background: rgba(48, 49, 51, 0.2);
        transition: opacity 0.2s;
        &:after {
          font-size: 16px;
        }
        &:hover {
          background: rgba(48, 49, 51, 0.3);
        }
      }
      .swiper-button-prev.swiper-button-disabled,
      .swiper-button-next.swiper-button-disabled {
        pointer-events: auto;
      }
      ::v-deep .swiper-pagination-bullet {
        width: 30px;
        height: 3px;
        background: #ffffff;
        opacity: 0.3;
        border-radius: 0;
      }
      ::v-deep .swiper-pagination-bullet-active {
        opacity: 1;
      }
      &:hover {
        .swiper-button-prev,
        .swiper-button-next {
          opacity: 1;
        }
      }
    }
    .news-recommend {
      height: 180px;
      position: relative;
      cursor: pointer;
      & + .news-recommend {
        margin-top: 10px;
      }
      .el-image {
        width: 100%;
        height: 180px;
        display: block;
      }
      .title {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.3);
        color: #fff;
        font-size: 14px;
        padding: 10px 6px;
        @include ellipsis();
      }
    }
  }
  .news-wrapper {
    margin-top: 10px;
    .news-all {
      background-color: #fff;
      padding-bottom: 40px;
      .news-category {
        padding: 20px 15px 10px;
      }
      .all-list {
        margin-top: 20px;
        padding: 0 10px;
        .item {
          padding: 12px;
          margin-bottom: 16px;
          &:hover {
            box-shadow: 0px 0px 14px 0px rgba(0, 0, 0, 0.08);
          }
          .picture {
            height: 0;
            width: 221px;
            position: relative;
            padding-top: 122px;
            cursor: pointer;
            .el-image {
              position: absolute;
              left: 0;
              top: 0;
              width: 100%;
              height: 100%;
            }
          }
          .content {
            flex: 1;
            margin-left: 16px;
            padding: 7px 0;
            .title {
              font-size: 20px;
              color: $text-color-import;
              cursor: pointer;
              @include ellipsisLine(1);
            }
            .abstract {
              margin-top: 15px;
              height: 40px;
              font-size: 13px;
              color: $text-color-secondary;
              line-height: 20px;
              letter-spacing: 1px;
              @include ellipsisLine(2);
            }
            .desc {
              margin-top: 20px;
              font-size: 13px;
              color: $text-color-auxiliary;
              line-height: 18px;
            }
          }
        }
      }
    }
    .news-hot {
      padding: 20px;
      background-color: #fff;
      .hot-title {
        font-size: 22px;
        color: $text-color-import;
      }
      .hot-list {
        margin-top: 10px;
        .item {
          align-items: flex-start;
          .index {
            width: 50px;
            font-size: 26px;
            font-weight: bold;
            color: #dcdfe6;
            padding-top: 20px;
          }
          & + .item .content {
            border-top: 1px solid $border-color-divider;
          }
          .content {
            flex: 1;
            padding: 20px 0;
            .title {
              font-size: 14px;
              color: $text-color-import;
              cursor: pointer;
              @include ellipsisLine(1);
              &:hover {
                @include edu-color-auxiliary;
              }
            }
            .abstract {
              margin-top: 10px;
              font-size: 13px;
              color: $text-color-secondary;
              line-height: 20px;
              letter-spacing: 1px;
              @include ellipsisLine(2);
            }
            .desc {
              margin-top: 15px;
              font-size: 12px;
              color: $text-color-auxiliary;
            }
          }
        }
      }
    }
  }
}
</style>
